#include "MapperCNROM.h"
#include "Log.h"

PNE_NAMESPACE_BEGIN

MapperCNROM::MapperCNROM(Cartridge &cart)
    : Mapper(cart, Mapper::Type::CNROM),
      m_selectCHR(0)
{
    if (cart.GetROM().size() == 0x4000) // 1 bank
    {
        m_oneBank = true;
    }
    else // 2 bank
    {
        m_oneBank = false;
    }
}

Byte MapperCNROM::ReadPRG(Address addr)
{
    if (!m_oneBank)
    {
        return m_cartridge.GetROM()[addr - 0x8000];
    }
    else
    {
        return m_cartridge.GetROM()[(addr - 0x8000) & 0x3fff];
    }
}

void MapperCNROM::WritePRG(Address addr, Byte value)
{
    m_selectCHR = value & 0x3;
}

Byte MapperCNROM::ReadCHR(Address addr)
{
    return m_cartridge.GetVROM()[addr | (m_selectCHR << 13)];
}

void MapperCNROM::WriteCHR(Address addr, Byte value)
{
    LOG(Level::Info) << "Read-only CHR momery write attempt at " << std::hex << addr << std::endl;
}
PNE_NAMESPACE_END

// DONE